<template>
  <section>
    <van-sticky>
      <PageHeader title="活动"/>
    </van-sticky>

    <van-list
      class="mt10 pd10"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="requestData">
      <div class="flex flex-col bg-color-white mb10 font-common round-box activity-item"
           v-for="(item, index) in dataList"
           :key="index"
            @click="$router.push({name: 'ActivityDetail', query: {id: item.id}})">
        <div class="flex flex-row flex-center">
          <img :src="item.img_url" class="w100 img" />
        </div>
        <div class="flex flex-row flex-left pd10 font-biger strong">
          {{item.name}}
        </div>
        <div class="flex flex-row flex-left pl10 pr10 pb10">
          活动时间：{{item.begin_time|timeFormat('yyyy-MM-dd')}}<i class="iconfont icon-location ml5 mr5" />{{item.region}}
        </div>
        <div class="flex flex-row flex-left pl10 pr10 pb10">
          报名截止：{{item.apply_deadline_time_str}}
        </div>
      </div>
    </van-list>

    <Tabbar active="activity"/>
  </section>
</template>

<script>
  import Vue from 'vue';
  import {Toast, Dialog, List, Search, Sticky, Tab, Tabs, Button} from 'vant';
  import {activityListsUri} from "../../common/api";
  import PageHeader from "../componets/PageHeader";
  import Tabbar from '../componets/Tabbar';
  Vue.use(Toast).use(Dialog).use(List).use(Search).use(Sticky).use(Tab).use(Tabs).use(Button);

  export default {
    components: {
      PageHeader,
      Tabbar
    },
    data() {
      return {
        currentPage: 1,
        lastPage: 0,
        total: 0,
        pageSize: 10,
        loading: false,
        finished: false,
        dataList: []
      }
    },
    methods: {
      requestData() {
        const that = this;
        this.loading = true;
        const url = activityListsUri + '?page_size=' + that.pageSize + '&page=' + that.currentPage;
        this.$http.get(url).then(response => {
          let {code, msg, data} = response.body
          if (code != 0) {
            that.toast(msg);
          } else {
            that.loading = false;
            if (that.currentPage >= data.last_page) {
              that.finished = true;
            }
            that.currentPage = that.currentPage + 1;
            for (let i = 0; i < data.data.length; i++) {
              that.dataList.push(data.data[i]);
            }
            that.total = data.total;
            that.lastPage = data.last_page;
          }
        }, response => {
          // error callback
        })
      }
    },
    mounted: function () {

    }
  }
</script>

<style scoped>
  .activity-item .img {
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }
</style>
